// src/plugins/auth.js
import Vue from "vue";
import Vuex from "vuex";
import router from "../router";
Vue.use(Vuex);
import axios from "axios";

export default {
  install(Vue) {
    Vue.prototype.$auth = {
      login(username, password) {
        // 在此处编写用户登录验证逻辑
        const promise = axios.post('login/',{
                username: username,
                password: password
              });
        promise.then(res =>{
          console.log("res:",res)
          if (res["data"]["result"]=="success"){
            console.log("auth.js 登录成功")
            localStorage.setItem("token",res["data"]["token"])
            console.log(Promise.resolve())
            return Promise.resolve()
          } else {
            return Promise.reject(new Error('Invalid username or password'))
          }
        }
        )
      },
      logout() {
        // 在此处编写用户退出登录逻辑
        this.isAuthenticated = false
        return Promise.resolve()
      }
    }
  }
}
